package cn.book.admin.service;

import cn.book.admin.dao.BookDao;
import cn.book.admin.entity.BookInfo;
import cn.book.admin.entity.BookType;
import cn.book.admin.entity.Page;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true)
public class BookService {
    @Autowired
    private BookDao bookDao;

    public List<BookType> findTypes(){
        return bookDao.findTypes();
    }

    public Page<BookInfo> search(String name, int pageSize, int pageNo){
        List list=bookDao.search(name,pageSize,pageNo);
        int total=bookDao.searchTotal(name);
        int pageTotal=total/Page.PAGE_SIZE+(total%Page.PAGE_SIZE==0?0:1);
        Page<BookInfo> page=new Page<>();
        page.setData(list);
        page.setPageNo(pageNo);
        page.setPageTotal(pageTotal);
        return page;
    }


    public BookInfo get(long id){
        return bookDao.get(id);
    }
    @Transactional(readOnly = false)
    public int del(long id){
        return bookDao.del(id);
    }
    @Transactional(readOnly = false)
    public int update(BookInfo book){
        return bookDao.update(book);
    }
    @Transactional(readOnly = false)
    public int add(BookInfo b) {
        return  bookDao.add(b);
    }
}
